home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_08_09
/
8n09083a
< prev
next >
Wrap
Text File
|
1990-05-30
|
13KB
|
584 lines
/*
+--------------------------------------------------------------+
| @(#) bfs_cmon.c v1.0 90/04/02 |
| |
| Copyright (C) 1990, StonyBrook Technologies, Inc., |
| All Rights Reserved. |
| |
| This Module contains Proprietary Information of |
| StonyBrook Technologies, Inc. and should be treated |
| as Confidential. |
| |
+--------------------------------------------------------------+
bfs_cmon.c - this is a client server daemon.
*/
#include <stdio.h>
#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/signal.h>
#include <varargs.h>
#include <eicon/x25.h>
#include "bfs.h"
static int running = 1; /* running bit - can be modified on SIGINT */
char cname[16]; /* client name */
char laddr[16]; /* client addr */
int cfifo; /* client fifo */
int c_lcb_cnt = 0; /* line cntl blk count */
C_LCB c_lcb[MAX_C_LCB]; /* line cntl blk */
C_FCB c_fcb[MAX_C_FCB]; /* apl fifo cntl blk */
C_SCB c_scb[MAX_C_SCB]; /* session cntl blk */
/* x.25 call parameter data */
struct x25parm x25_parms = { 0, "", 0, "", "" };
/* debug stuff */
int cmon_io;
char cmon_buf[132];
int debug_flag = 0;
signal_trap(in_signal)
int in_signal;
{
if(in_signal == SIGTERM)
running = 0; /* terminate when requested by outside */
}
cleanup_exit(exval)
int exval;
{
cmon_term();
exit(exval);
}
cmon_init()
{
FILE *ifd;
char buf[132];
int i, foundme;
dopen();
for(i=0; i<MAX_C_LCB; i++) {
c_lcb[i].port = c_lcb[i].link = c_lcb[i].cid = 0;
c_lcb[i].sname[0] = '\0';
c_lcb[i].raddr[0] = '\0';
c_lcb[i].cbuf = c_lcb[i].rbuf = NULL;
}
for(i=0; i<MAX_C_FCB; i++)
c_fcb[i].afifo = c_fcb[i].pid = c_fcb[i].link = 0;
for(i=0; i<MAX_C_SCB; i++) {
c_scb[i].pid = c_scb[i].fid =
c_scb[i].lid = c_scb[i].ssid = 0;
c_scb[i].state = -1;
}
c_lcb_cnt = 0;
foundme = 0;
if((ifd=fopen("servers", "r")) != NULL) {
while(fgets(buf, 132, ifd)) {
if((buf[0] != '#') && (sscanf(buf, "%s %d %s",
c_lcb[c_lcb_cnt].sname,
&c_lcb[c_lcb_cnt].port,
c_lcb[c_lcb_cnt].raddr) > 0)) {
if(!foundme) {
foundme = 1;
strcpy(cname, c_lcb[c_lcb_cnt].sname);
strcpy(laddr, c_lcb[c_lcb_cnt].raddr);
} else
c_lcb_cnt++;
}
}
fclose(ifd);
} else {
strcpy(cname, "NONAMECLIENT");
strcpy(laddr, "12345");
}
cfifo_init();
cx25_init();
}
cmon_term()
{
cfifo_term();
cx25_term();
dprintf("BFS-CMON Terminating\n");
dclose();
}
main()
{
RPC_MSG rpc_msg;
int flags;
cmon_init();
dprintf("Basic File System - Client Monitor (BFS-CMON) Ver 1.0\n");
dprintf("Copyright (C) 1990, StonyBrook Technologies, Inc.\n");
dprintf("All Rights Reserved.\n");
print_info(); /* print routing table to debug file */
if(signal(SIGTERM, signal_trap) == -1) { /* setup signal handler */
perror("SIGTERM Failed");
exit(errno);
}
while( running ) {
if(cfifo_read(&rpc_msg, sizeof(RPC_MSG))) {
switch(rpc_msg.hdr.code) {
case RPC_FOPEN_CMD:
cmon_fopen(&rpc_msg);
break;
case RPC_FCLOSE_CMD:
cmon_fclose(&rpc_msg);
break;
case RPC_FWRITE_CMD:
cmon_fwrite(&rpc_msg);
break;
case RPC_FREAD_CMD:
cmon_fread(&rpc_msg);
break;
case RPC_FEOF_CMD:
cmon_feof(&rpc_msg);
break;
default:
break;
}
}
}
cmon_term();
}
/* cfifo_init, _term, _open, _close, _read, _write routines */
cfifo_init()
{
int flags;
if(mknod("/tmp/CFIFO", S_IFIFO|0666, 0) == -1) return(0);
if((cfifo=open("/tmp/CFIFO", O_RDONLY|O_NDELAY)) != -1) {
flags = fcntl(cfifo, F_GETFL, &flags);
flags &= ~O_NDELAY;
fcntl(cfifo, F_SETFL, &flags);
return(1);
}
unlink("/tmp/CFIFO");
return(0);
}
cfifo_term()
{
close(cfifo);
unlink("/tmp/CFIFO");
}
cfifo_open(pid)
int pid;
{
int i;
for(i=0; i<MAX_C_FCB; i++)
if((c_fcb[i].link) && (c_fcb[i].pid == pid)) {
c_fcb[i].link++;
return(1);
}
for(i=0; i<MAX_C_FCB; i++)
if(!c_fcb[i].link) {
sprintf(c_fcb[i].afname, "/tmp/AFIFO.%d", pid);
if((c_fcb[i].afifo=open(c_fcb[i].afname, O_WRONLY)) != -1) {
c_fcb[i].link = 1;
c_fcb[i].pid = pid;
return(1);
}
break;
}
return(0);
}
cfifo_close(pid)
int pid;
{
int i;
for(i=0; i<MAX_C_FCB; i++)
if(c_fcb[i].pid == pid) {
c_fcb[i].link--;
if(!c_fcb[i].link) {
close(c_fcb[i].afifo);
c_fcb[i].pid = c_fcb[i].afifo = -1;
}
return(1);
}
return(0);
}
cfifo_read(buf, cnt)
char *buf;
int cnt;
{
return(read(cfifo, buf, cnt));
}
cfifo_write(pid, buf, cnt)
int pid, cnt;
char *buf;
{
int i;
for(i=0; i<MAX_C_FCB; i++)
if(c_fcb[i].pid == pid)
return(write(c_fcb[i].afifo, buf, cnt));
return(0);
}
/* cx25_init, _term, _open, _close,
_send_cmd, _send_data, _recv_rsp, _recv_data */
cx25_init()
{
x25init(0);
}
cx25_term()
{
x25exit();
}
cx25_open(lid)
int lid;
{
if(!c_lcb[lid].link) {
dprintf("open x25 line %d\n", lid);
if(x25call(c_lcb[lid].raddr, laddr, &x25_parms,
c_lcb[lid].port, X25WAIT, &c_lcb[lid].cid) == -1) {
dprintf("x25 open error %d on line %d\n", x25error(), lid);
return(0);
}
dprintf("x25 line %d opened ok\n", lid);
c_lcb[lid].link = 1;
c_lcb[lid].cbuf = x25alloc(sizeof(RPC_FOPEN_CMD_MSG));
c_lcb[lid].rbuf = x25alloc(sizeof(RPC_FOPEN_RSP_MSG));
} else {
dprintf("link x25 line %d\n", lid);
c_lcb[lid].link++;
}
return(1);
}
cx25_close(lid)
int lid;
{
dprintf("delink x25 line %d\n", lid);
c_lcb[lid].link--;
if(!c_lcb[lid].link) {
dprintf("close x25 line %d\n", lid);
x25free(c_lcb[lid].cbuf);
x25free(c_lcb[lid].rbuf);
}
return(1);
}
cx25_send_cmd(lid, buf, cnt)
int lid, cnt;
char *buf;
{
memcpy(c_lcb[lid].cbuf, buf, cnt);
cnt = x25send(c_lcb[lid].cid, c_lcb[lid].cbuf, cnt, 0);
return(cnt);
}
cx25_send_data(lid, pid, cnt)
int lid, pid, cnt;
{
c_lcb[lid].dbuf = x25alloc(cnt);
cfifo_read(c_lcb[lid].dbuf, cnt);
cnt = x25send(c_lcb[lid].cid, c_lcb[lid].dbuf, cnt, 0);
x25free(c_lcb[lid].dbuf);
return(cnt);
}
cx25_recv_rsp(lid, buf, cnt)
int lid, cnt;
char *buf;
{
cnt = x25recv(c_lcb[lid].cid, c_lcb[lid].cbuf, cnt, 0);
memcpy(buf, c_lcb[lid].cbuf, cnt);
return(cnt);
}
cx25_recv_data(lid, pid, cnt)
int lid, pid, cnt;
{
c_lcb[lid].dbuf = x25alloc(cnt);
cnt = x25recv(c_lcb[lid].cid, c_lcb[lid].dbuf, cnt, 0);
cfifo_write(pid, c_lcb[lid].dbuf, cnt);
x25free(c_lcb[lid].dbuf);
return(cnt);
}
/* cmon_fopen, _fclose, _fread, _fwrite, _feof routines */
cmon_fopen(rpc_msg)
RPC_MSG *rpc_msg;
{
int pid, fid, lid, csid;
RPC_FOPEN_CMD_MSG focmd;
RPC_FOPEN_RSP_MSG forsp;
cfifo_read(focmd.sname, rpc_msg->hdr.dlen);
pid = rpc_msg->hdr.csid;
fid = rpc_msg->hdr.ssid;
lid = get_server_id(focmd.sname);
if(cfifo_open(pid)) {
if(lid != -1 ) {
dprintf("fopen req for %s - ok\n", focmd.sname);
if(cx25_open(lid)) {
if((csid=allocate_csid(pid, fid, lid)) != -1) {
focmd.hdr.code = RPC_FOPEN_CMD;
focmd.hdr.csid = csid;
focmd.hdr.ssid = 0;
focmd.hdr.dlen = rpc_msg->hdr.dlen;
cx25_send_cmd(l